package LAGD;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DebugGraphics;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class ProgettoGUI extends JFrame {
	
	
	 JButton registraUtente;
	 JButton utenteLibero;
	 JButton login;
	 login log;
	 registraUtente rg;
	 JButton registraUtente1;
	 List<Utente> utente;
	 Utente u=null;
	 public int n;
	 public double prezzoTotale=0;
	 public static FinestraRichiestaAdmin fra;
	// MainProgetto mp=new MainProgetto();
	 FinestraCerca fc;
	 
	 public static Connection conn;
	
	AscoltatoreEventiAzione listener=new AscoltatoreEventiAzione(); 
	public MainProgetto mp=new MainProgetto();

	private JPanel contentPane;
	static String user;
	private String password;
	 String codiceFiscale, nomeUtente, indirizzo, passwordU,cognomeUtente,dataNascita;
	private JButton admin;

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		/*
		String drivers = "com.mysql.jdbc.Driver";
		System.setProperty("jdbc.drivers", drivers);
		String url = "jdbc:mysql://localhost:3306/blockbuster";
		String username = "root";
		String password = "napoleone2";
		*/
		/*String pFile="database.properties";
		Properties props=new Properties();
		
		try {
			FileInputStream in=new FileInputStream(pFile);
			props.load(in);
			in.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		String drivers=props.getProperty("jdbc.drivers");
		String url = props.getProperty("jdbc.url");
		String username = props.getProperty("jdbc.username");
		String password = props.getProperty("jdbc.password");
		
		try {
			conn= DriverManager.getConnection(url, username,
					password);
			//ProgettoGUI progettoGUI=new ProgettoGUI(0);
			//progettoGUI.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
			
			
		//conn.close();
	}*/
	

	/**
	 * Create the frame.
	 */
	public ProgettoGUI(final Connection conn, int n) {
		this.conn=conn;

		//setType(Type.POPUP);
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(ProgettoGUI.class.getResource("/Graphics/architetto-utente-singolo_t.png")));
		///setSize(300,300);
		setBounds(100, 100, 450, 200);
		contentPane = new JPanel();
		contentPane.setBackground(Color.GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.GRAY);
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		utenteLibero = new JButton("Utente Libero");//("Utente Libero");
		utenteLibero.setMnemonic('u');
		utenteLibero.setToolTipText("Selezionare per entrare nella finestra per l'utente libero o per cercare il film di proprio interesse");
		utenteLibero.setBounds(20,23, 380, 34);
		//utenteLibero.setBounds(20, 78, 380, 34);
		utenteLibero.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		//utenteLibero.setBounds(21, 30, 380, 34);
		if(n==2)
		panel.add(utenteLibero);
		utenteLibero.addActionListener(listener);
		
		registraUtente = new JButton("Registra Utente");

		registraUtente.setBounds(20, 135, 380, 34);
		//registraUtente.setBounds(21, 105, 380, 34);
		//panel.add(registraUtente);
		registraUtente.addActionListener(listener);
		
		if(n==1){
			this.setVisible(false);
			ControlloUtente c=new ControlloUtente(conn);
			c.setVisible(true);
		admin = new JButton ("Admin");
		admin.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		admin.setBounds(20,23, 380, 34);
		//panel.add(admin);
		admin.addActionListener(listener);
		}
		
		login = new JButton("Log-in");
		login.setMnemonic('l');
		login.setToolTipText("Selezionare per connettere un account");
		login.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		login.addActionListener(listener);//(new ActionListener() {
			//public void actionPerformed(ActionEvent arg0) {
		//	}
		//});
		login.setBounds(20, 78, 380, 34);
		//login.setBounds(20, 192, 380, 34);
		//login.setBounds(21, 180, 380, 34);
		if(n==2){
		panel.add(login);
		}
		this.setTitle("Gestione Block Buster");
		
		JLabel sfondo=new JLabel("");
		sfondo.setIcon(new ImageIcon(ProgettoGUI.class.getResource("/Graphics/finestracerca.png")));
		sfondo.setLabelFor(this);
		sfondo.setBounds(0, 0, 440, 200);
		panel.add(sfondo);
		
		
		
	}	
	private class AscoltatoreEventiAzione implements ActionListener {
		public void actionPerformed(ActionEvent e) {
				if(e.getSource()==admin){
					ControlloUtente cu=new ControlloUtente(conn);
					cu.setVisible(true);
					/*fa= new FinestraAdmin();
					fa.setVisible(true);
				*/}
			
			
			if(e.getSource() == registraUtente){
				rg=new registraUtente();
				rg.setVisible(true);
				
				//int selectedIndex = magazzinoList.getSelectedIndex();
				/*try{
					Scorta scorta = magazzinoList.getModel().getElementAt(selectedIndex);
					double newValue = Double.parseDouble(qtaTxt.getText());
					controller.updateIngrediente(scorta.getIngrediente(), newValue);
					controller.updateMagazzinoList(magazzinoList);
					controller.updatePizzeBtnStatus(pizzeBtn, nuovoOrdineBtn, confermaOrdine);
					qtaTxt.setText("");
				}catch(Exception ex){System.out.println("Errore: "+ex);};
			} */}
			else if(e.getSource() == utenteLibero){
				n=0;
				FinestraCerca p=new FinestraCerca(conn,n);
				p.setVisible(true);
				
				
				/*controller.nuovoOrdine();
				controller.updatePizzeBtnStatus(pizzeBtn, nuovoOrdineBtn, confermaOrdine);	
				controller.updateLabelCostoOrdine(totaleLbl);*/
			}
			else if(e.getSource() == login){
				//System.out.println(1);
				
				//JFrame f=new JFrame()
				/*JTextField codice=new JTextField(16);
				JTextField password=new JTextField(20);
				JOptionPane.showInputDialog(codice,password);
				*/
				/*String o=new String();
				String l=new String();*/
				log=new login();
				log.setVisible(true);
				/*JOptionPane.showInputDialog(o, l);
				System.out.println("id: "+o+ "password: "+l);
				/*controller.salvaOrdine();
				controller.updatePizzeBtnStatus(pizzeBtn,  nuovoOrdineBtn,  confermaOrdine);
				controller.updateLabelCostoOrdine(totaleLbl);
				tModel.reset();*/
			}
			}
	};
	
	public class ControlloUtente extends JFrame implements ActionListener{
		 private JTextField source1;
	   	 private JButton ok, annulla;
	   	 private String gui;	
	   	 private String a="napoleone2";
	   
	   	
		
		public ControlloUtente(Connection conn){
			setResizable(false);
			setIconImage(Toolkit.getDefaultToolkit().getImage(ProgettoGUI.class.getResource("/Graphics/Senza titolo-2.png")));
			setTitle("controllo");
			
			setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			JPanel p=new JPanel();
			p.setLayout( new GridBagLayout() );
	   	    GridBagConstraints gb = new GridBagConstraints();
	   	    gb.gridx = gb.RELATIVE;
	   	   
	   	    p.add(new JLabel("Password", JLabel.RIGHT), gb);
   	     	p.add(source1=new JPasswordField("",20), gb);
   	     	gb.gridy = 1;
   	     	gb.gridwidth=0;
   	     	annulla=new JButton("Indietro");
   	     annulla.setMnemonic('i');
 		//annulla.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
 		
   	     	ok=new JButton("OK");
   	     ok.setMnemonic('o');
 		//annulla.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
   	     	//annulla=new JButton("");
   	     	//ok=new JButton("");
   	     	JPanel ct=new JPanel();
   	     	ct.setLayout(new FlowLayout());
   	     	ct.add(annulla);
   	     	ct.add(ok);
   	     
   	     	p.add(ct,gb);
   	     add(p);	 
   	     source1.addActionListener(this);
   	  	//ok.setPressedIcon(new ImageIcon(Prova.class.getResource("/Graphics/NextMenuButtonIcon.png")));
		ok.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		ok.setMnemonic('o');
		//admin.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
		//ok.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
		//ok.setBorderPainted(false);
		//ok.setIcon(new ImageIcon(Prova.class.getResource("/Graphics/NextMenuButtonIconSubpictur.png")));
		ok.setIcon(new ImageIcon(ProgettoGUI.class.getResource("/Graphics/ok.png")));
		
   		 ok.addActionListener(this);
   		//annulla.setPressedIcon(new ImageIcon(Prova.class.getResource("/Graphics/indietro.png")));
		annulla.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		//annulla.setMnemonic('a');
		//admin.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
		//annulla.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
		annulla.setIcon(new ImageIcon(ProgettoGUI.class.getResource("/Graphics/indietro.png")));
		//annulla.setIcon(new ImageIcon(Prova.class.getResource("/Graphics/ics.png")));
		annulla.addActionListener(this);
   		 setLocation(250,340);
   		 setSize(400,150);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			
			
			if( e.getSource()==source1 ){
				gui=source1.getText();
				 
			 }
			if( e.getSource()==annulla ){
				 this.setVisible(false);
				 				 
			 }
			try{
			 if( e.getSource()==ok ){
				 this.setVisible(false);
				 if(!gui.equals(a)){
					 JOptionPane.showMessageDialog(null, "Password Sbagliata");
					 this.setVisible(true);
					 System.out.println(source1.getText());
					 System.out.println(gui);
					 System.out.println(a);
				 	
				 }else {
					 fra=new FinestraRichiestaAdmin((com.mysql.jdbc.Connection) conn);
					 fra.setVisible(true);
				 } 
			}	
			}catch(NullPointerException o){
				JOptionPane.showMessageDialog(null, "Inserire una password valida");
				this.setVisible(true);
			}
		}
		
	}
	public class login extends JFrame implements ActionListener{
	   	 private JTextField source2,source1;
	   	 private JButton ok, annulla;
	   	 
	   	  public login(){
	   		setResizable(false);
	   		setIconImage(Toolkit.getDefaultToolkit().getImage(ProgettoGUI.class.getResource("/Graphics/architetto-utente-singolo_t.png")));
	   		setTitle("Log - in");
	   		 setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	   	     //addWindowListener( new WindowAdapter() {
			//	public void windowClosing(WindowEvent e){
					
			//	}
	   		  //   });//windowClosing
	   	     
	   	     
	   	     JPanel p=new JPanel();
	   	     p.setLayout( new GridBagLayout() );
	   	     GridBagConstraints gb = new GridBagConstraints();
	   	     gb.gridx = gb.RELATIVE;
	   	    
	   	     p.add( new JLabel("Codice Fiscale ", JLabel.RIGHT), gb );
	   	     p.add(source1=new JTextField("",16), gb );
	   	     gb.gridy = 1;
	   	     p.add(new JLabel("Password", JLabel.RIGHT), gb);
	   	     p.add(source2=new JPasswordField("",20), gb);
	   	     gb.gridy = 2;
	   	     gb.gridwidth=0;
	   	     annulla=new JButton("Annulla");
	   	  annulla.setIcon(new ImageIcon(ProgettoGUI.class.getResource("/Graphics/indietro.png")));
	   	annulla.setMnemonic('a');
		//admin.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
	   	  annulla.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	   	     ok=new JButton("OK");
	   	  ok.setIcon(new ImageIcon(ProgettoGUI.class.getResource("/Graphics/ok.png")));
	   	ok.setMnemonic('o');
		//admin.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
	   	     registraUtente1=new JButton("Registra Utente");
	   	  registraUtente1.setMnemonic('u');
			registraUtente1.setToolTipText("Selezionare per registrare un utente");
	   	     ok.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	   	     registraUtente1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	   	      JPanel ct=new JPanel();
			ct.setLayout(new FlowLayout());
			ct.add(annulla);
			ct.add(ok);
			ct.add(registraUtente1);
			p.add(ct,gb);
	   	     
	   	    /* p.add(annulla=new JButton("Annulla"),gb );
	   	     gb.gridx=1;
	   	     p.add(ok=new JButton("OK"), gb );
	   	     gb.gridy= 2;
	   	     */
	   	    
	   	     
	   	     add(p);	 
	   	     source1.addActionListener(this);
	   		 source2.addActionListener(this);
	   		 ok.addActionListener(this);
	   		 annulla.addActionListener(this);
	   		 registraUtente1.addActionListener(this);
	   		 
	   		 setLocation(250,340);
	   		 setSize(400,150);
	   		 
	   	 }
	   	  
	   public void actionPerformed( ActionEvent e ){
		   
		   if( e.getSource()==source1 ){
				 ProgettoGUI.this.user=source1.getText();
			 }
			 if( e.getSource()==source2 ){
				 ProgettoGUI.this.password=source2.getText();
				 				 
			 }
			 if( e.getSource()==annulla ){
				 this.setVisible(false);
				 				 
			 }
			 if(e.getSource()==registraUtente1){
				 this.setVisible(false);
				 registraUtente u=new registraUtente();
				 u.setVisible(true);
			 }
			 
			 else if( e.getSource()==ok ){
				 this.setVisible(false);

				 if((user==null)||user.length()==0){
					 if(password==null||password.length()!=0){
					 log.setVisible(true);
					 }
					 if(password==null||password.length()==0){ //password==null
						 
						 log.setVisible(true);
						 
					 }
				 } 
				 else{//(user!=null){	//user!=null
					 if(password==null||password.length()==0){				 
					 log.setVisible(true);
				 } if(password.length()!=0) {
					 
					try {
						utente = mp.cercaUtentePreciso(conn, user);
						System.out.println(utente.toString());
							u=utente.get(0);
							System.out.println(u.getPassword());
							System.out.println(password);
					} catch (SQLException e1) {						
						e1.printStackTrace();
					}
					if(utente.size()!=0&&password.equals(u.getPassword())){
						n=1;
						FinestraCerca ful=new FinestraCerca(conn,n);				 
						ful.setVisible(true);
					} else if(utente.size()!=0&&!password.equals(u.getPassword())){ 
						JOptionPane.showMessageDialog(null, "Password Sbagliata");
						//login log=new login();
						this.setVisible(true);
					}
					else {
						JOptionPane.showMessageDialog(null, "L'Utente non esiste");
						//login log=new login();
						this.setVisible(true);
					}
			 	}
				 
				 }}
				 //System.out.println("premuto ok");
			 }
	   public String getCodiceFiscale(){
			String codiceFiscale=source1.getText();			
			return codiceFiscale;
		}
	   public String getPassword(){
			String password=source2.getText();			
			return password;
		}
	}
	   	  
	public class registraUtente extends JFrame implements ActionListener{
		private JTextField source1, source2, source3,source5,source6;
		private JButton annulla, ok;
		private JTextField source4;
		
		public String getCodiceFiscale(){
			String codiceFiscale=source1.getText();			
			return codiceFiscale;
		}
		public String getPassword(){
			String password=source4.getText();				
			return password.toString();
		}
		
		
		public registraUtente(){
			setIconImage(Toolkit.getDefaultToolkit().getImage(ProgettoGUI.class.getResource("/Graphics/architetto-utente-singolo_t.png")));
			setResizable(false);
			setTitle("Registra Utente");
			setDefaultCloseOperation(HIDE_ON_CLOSE);
			JPanel p=new JPanel();
			GridBagConstraints gb = new GridBagConstraints();
	   	    gb.gridx = gb.RELATIVE;
			
			p.setLayout(new GridBagLayout());
			p.add( new JLabel("Codice Fiscale ", JLabel.RIGHT), gb);
			//gb.gridx = 1;
			p.add(source1=new JTextField("",16), gb);
			//gb.gridx = 2;
			gb.gridy = 1;
			p.add(new JLabel("Nome" , JLabel.RIGHT), gb);
			//gb.gridx = 1;
			p.add(source2=new JTextField("",20), gb);
			//gb.gridx = 2;
			gb.gridy = 2;
			p.add(new JLabel("Cognome" , JLabel.RIGHT), gb);
			p.add(source5=new JTextField("",20), gb);
			gb.gridy=3;
			p.add(new JLabel("Indirizzo" , JLabel.RIGHT), gb);
			//gb.gridx = 1;
			p.add(source3=new JTextField("",45), gb);
			gb.gridy = 4;
			
			p.add(new JLabel("Data Nascita" , JLabel.RIGHT), gb);
			p.add(source6=new JTextField("",20), gb);
			//gb.gridx = 2;
			gb.gridy = 5;
			p.add(new JLabel("Password" , JLabel.RIGHT), gb);
			//gb.gridx = 1;
			p.add(source4=new JPasswordField("",20), gb);
			
			gb.gridy = 6;
			gb.gridwidth = 0;
			
			annulla=new JButton("Annulla");
			annulla.setMnemonic('a');		
			annulla.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));			
			ok=new JButton("Ok");
			ok.setMnemonic('o');
			ok.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			//admin.setToolTipText("Selezionare per entrare nella gestione del blockbuster");
			JPanel ct=new JPanel();
			ct.setLayout(new FlowLayout());
			ct.add(annulla);
			ct.add(ok);
			p.add(ct,gb);
			add(p);
			setLocation(250,340);
	   		setSize(610,150);
	   		pack();
	   		source5.addActionListener(this);
	   		source6.addActionListener(this);
	   		source1.addActionListener(this);
	   		 source2.addActionListener(this);
	   		source3.addActionListener(this);
	   		source4.addActionListener(this);
	   		 ok.addActionListener(this);
	   		 annulla.addActionListener(this);
		}

		public void actionPerformed(ActionEvent e) {
			 if( e.getSource()==source1 ){
				 ProgettoGUI.this.codiceFiscale=source1.getText();
				 System.out.println(codiceFiscale);
			 }
			 if( e.getSource()==source2 ){
				 ProgettoGUI.this.nomeUtente=source2.getText();
				 System.out.println(nomeUtente);
				 				 
			 }
			 if( e.getSource()==source3 ){
				 ProgettoGUI.this.indirizzo=source3.getText();
				 System.out.println(indirizzo);
			 }
			 if( e.getSource()==source4 ){
				 ProgettoGUI.this.passwordU=source4.getText();
				 System.out.println(passwordU);
				 				 
			 }
			 if( e.getSource()==annulla ){
				 this.setVisible(false);
				 System.out.println("premuto annulla");
				 				 
			 }
			 if(e.getSource()==source5){
				 ProgettoGUI.this.cognomeUtente=source5.getText();
				 
			 }
			 if(e.getSource()==source6){
				 ProgettoGUI.this.dataNascita=source6.getText();
			 }
			 else if( e.getSource()==ok ){
				 this.setVisible(false);
				 Utente u=new Utente(codiceFiscale, nomeUtente, cognomeUtente,indirizzo,dataNascita, null, null, passwordU);
				 try {
					mp.inserisciUtente(conn, u);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			 }
		 }			
		}
	}